<?php if ( ! defined('WDK_PATH')) exit('No direct script access allowed');
require_once BASE_PATH.'app/controller.php';
class Post extends Controller
{
	private $bbs;
	function __construct()
	{
		parent::__construct();
		$this->_init();
	}
	
	protected function _init() 
	{	
		$this->bbs = Wdk::get('Bbs');	
	}
	
	private function _loadBbs($bbs_id='') 
	{	
		if($bbs_id != '' && $this->bbs['bbs_id'] === $bbs_id)	return;
		try
		{
			if(trim($bbs_id) === '') throw new NoRecordException("");
			$this->bbs->loadById($bbs_id);
			$this->bbs->setUserSession($this->session);
		}
		catch(NoRecordException $e)
		{
			parent::_script(array("alert('존재하지 않는 게시판입니다.')", "history.back();"));
		}
	}
	
	public function index($bbs_id='', $page=1, $fk_category_seq=0, $sf='0', $sk='0')
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		if($sf == '0') $sf = '';
		if($sk == '0') $sk = '';
		$assign_data = array('bbs'=>$this->bbs, 'sf'=>$sf,'sk'=>$sk, 'page'=>$page);
		/****************************************************************************************************************************/
		try 
		{
			$this->_loadBbs($bbs_id);
			if($this->bbs->isListable() == false)	throw new BbsPermissionException();
			$assign_data['json'] = $this->json(true, $bbs_id, $page, $fk_category_seq, $sf, $sk);
		}
		catch(BbsAccessException $e)
		{
			parent::_script(array("alert('잘못된 접근입니다.')", "history.back();"));
		}
		catch(BbsPermissionException $e)
		{
			parent::_script(array("alert('접근 권한이 없습니다.')", "history.back();"));
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_view($assign_data, "/post/{$this->bbs['bbs_skin']}/index.html");
		/****************************************************************************************************************************/
	}
	
	public function json($return=false, $bbs_id='', $page=1, $fk_category_seq=0, $sf='', $sk='')
	{
		/*****************************************************************************************************************************
		 * POST PARAMS..
		*****************************************************************************************************************************/
		$page				= (int)		post('page', $page);
		$sf					= (string)	post('sf', $sf);
		$sk					= (string)	post('sk', $sk);
		$bbs_id				= (string)	post('bbs_id', $bbs_id);
		$fk_category_seq	= (int)	post('category_seq', $fk_category_seq);
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$this->_loadBbs($bbs_id);
		$listsize	= $this->bbs['bbs_listsize'];
		$pagesize	= $this->bbs['bbs_pagesize'];
		$lm = Wdk::get('PostManager');
		$lists = array();
		$where = array("`p`.`fk_bbs_seq` = {$this->bbs['bbs_seq']}");
		$assign_data = array('listsize'=>$listsize,'pagesize'=>$pagesize);
		/****************************************************************************************************************************/
		try
		{
			if(strlen($sf) > 0 && strlen($sk) > 0)
			{
				$where[] = "`{$sf}` LIKE '%{$lm['dbo']->escape($sk)}%'";
			}
			$lm['listsize'] = $listsize;
			$lm['page'] = $page;
			$lm['where'] = $where;
			$lm->load();
			
			foreach($lm['lists'] as $idx=>$list)
			{
				$data = array(	'no'				=>	$lm['total'] - ($page-1)*$listsize - $idx,
								'seq'				=>	$list['post_seq'],
								'user_nick'			=>	$list['user_nick_str'],
								'user_login_id'		=>	$list['user_login_id'],
								'title'				=>	$list['post_title'],
								'hits'				=>	$list['post_hits'],
								'comment_num'		=>	$list['comment_num'],
								'insert_time'		=>	$list['post_insert_time_str']
							);
				if($this->bbs['bbs_category_flag'] == 'T')
				{
					$data['category_name']	= $list['category_name']; 
				}
				if($this->bbs['bbs_recommend_flag'] == 'T')
				{
					$data['agree']		= $list['post_agree']; 
					$data['opposite']	= $list['post_opposite']; 
				}
				array_push($lists, $data);
			}
			$assign_data['total'] = $lm['total'];
			$assign_data['lists'] = $lists;
		}
		catch(NoRecordException $e)
		{
			$assign_data['total'] = 0;
			$assign_data['lists'] = array();
		}
		catch(WException $e)
		{
			$assign_data['error'] = true;
			$assign_data['msg'] = "Error";
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/
		return parent::_json($assign_data, $return);
		/****************************************************************************************************************************/
	}
	
	public function edit($bbs_id='', $post_seq=0)
	{
		$this->_edit($bbs_id, $post_seq);
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data = array('bbs'=>$this->bbs);
		/****************************************************************************************************************************/
		try 
		{
			$this->_loadBbs($bbs_id);
			if($this->bbs->isWritable() == false) throw new BbsPermissionException();
			$post = Wdk::get('Post');
			if($post_seq > 0)
			{
				$post->load($post_seq, $this->bbs);
				if($this->bbs['bbs_seq'] != $post['fk_bbs_seq'])	throw new BbsAccessException();
				if($post->isEditable() == false)	throw new BbsPermissionException();
				$assign_data['data'] = $post;
			}
		}
		catch(NoRecordException $e)
		{
			parent::_script(array("alert('존재하지 않는 게시물입니다.')", "history.back();"));
		}
		catch(BbsAccessException $e)
		{
			parent::_script(array("alert('잘못된 접근입니다.')", "history.back();"));
		}
		catch(BbsPermissionException $e)
		{
			parent::_script(array("alert('접근 권한이 없습니다.')", "history.back();"));
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_view($assign_data, "/post/{$this->bbs['bbs_skin']}/edit.html");
		/****************************************************************************************************************************/
	}
	
	private function _edit($bbs_id='', $post_seq=0)
	{
		if(parent::_isPost() == false) return;
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$captcha			= Wdk::helper('captcha');
		$bbs_id				= (string) post('bbs_id');
		$post_seq			= (int) post('post_seq', 0);
		$post_title			= (string) post('post_title', '');
		$post_ref_url		= (string) post('post_ref_url', '');
		$post_secure_flag	= (string) post('post_secure_flag', 'F');
		$post_user_name		= (string) post('post_user_name', '');
		$post_user_email	= (string) post('post_user_email', '');
		$post_user_mobile	= (string) post('post_user_mobile', '');
		$post_user_passwd	= (string) post('post_user_passwd', '');
		$post_content		= (string) post('post_content', '');
		$post_captcha_code	= (string) post('post_captcha_code', '');
		$post_option_field1	= (string) post('post_option_field1', '');
		$post_option_field2	= (string) post('post_option_field2', '');
		$post_option_field3	= (string) post('post_option_field3', '');
		$post_option_field4	= (string) post('post_option_field4', '');
		$post_option_field5	= (string) post('post_option_field5', '');
		$post_tag			= (string) post('post_tag', '');
		$uploader 			= Wdk::helper('Uploader');
		$file_removed		= array();
		$file_to_move		= array();
		/****************************************************************************************************************************/
		try 
		{
			$this->_loadBbs($bbs_id);
			if($this->bbs->isWritable() == false) throw new BbsPermissionException();
			if(parent::_isLogin() == false && $captcha->check($post_captcha_code) == false)
			{
				throw new PostCaptchaException();
			}
			$post = Wdk::get('Post');
			if($post_seq > 0)
			{
				$post->load($post_seq, $this->bbs);
				if($post->isEditable() == false)	throw new BbsPermissionException();
			}
			$post['fk_bbs_seq']			= $this->bbs['bbs_seq'];
			$post['fk_user_seq']		= $this->session['user_seq'];
			$post['post_title']			= $post_title;
			$post['post_ref_url']		= $post_ref_url;
			$post['post_secure_flag']	= $post_secure_flag;
			$post['post_user_name']		= $post_user_name;
			$post['post_user_email']	= $post_user_email;
			$post['post_user_mobile']	= $post_user_mobile;
			$post['post_user_passwd']	= $post_user_passwd;
			$post['post_content']		= $post_content;
			$post['post_option_field1']	= $post_option_field1;
			$post['post_option_field2']	= $post_option_field2;
			$post['post_option_field3']	= $post_option_field3;
			$post['post_option_field4']	= $post_option_field4;
			$post['post_option_field5']	= $post_option_field5;
			$post['post_tag']			= $post_tag;
			/********************************************************************************/
			$remove_post_file = (array) post('remove_post_file', array());
			if(empty($remove_post_file) == false)
			{
				$post_files = $post['post_files'];
				foreach($remove_post_file as $file_index)
				{
					$file_removed[] = $post['post_files'][$file_index]['path'];
					unset($post_files[$file_index]);
				}
				$post['post_files'] = array_values($post_files);
			}
			$uploader->upload_size = $this->bbs['bbs_file_size'];
			$uploader->upload_exts = $this->bbs['bbs_file_type']*1024;
			if($uploader->load('post_file', 'bbs'))
			{
				$upfiles = $uploader->get('post_file');
				foreach($upfiles as $file)	$file_to_move[] = $file['path'];	
				$post['post_files'] = array_merge($post['post_files'], $upfiles);				
			}
			/********************************************************************************/
			$post->autoUpdate();
			moveToFtp($file_to_move);
			removeFromFtp($file_removed);
		}
		catch(BbsAccessException $e)
		{
			parent::_script(array("alert('잘못된 접근입니다.')", "history.back();"));
		}
		catch(BbsPermissionException $e)
		{
			parent::_script(array("alert('접근 권한이 없습니다.')", "history.back();"));
		}
		catch(PostCaptchaException $e)
		{
			parent::_script(array("alert('입력보완코드값이 틀립니다.')", "history.back();"));
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_redirect('/post/view/'.$this->bbs['bbs_id'].'/'.$post['post_seq']);
		/****************************************************************************************************************************/
	}
	
	public function view($bbs_id='', $post_seq=0, $page=1, $category_seq=0, $sf='0', $sk='0')
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data	= array('params'=>array('page'=>$page, 'category_seq'=>$category_seq, 'sf'=>$sf, 'sk'=>$sk));
		$post 		= Wdk::get('Post');
		$bpv			= (array) $this->session['bpv'];
		/****************************************************************************************************************************/
		try 
		{
			$this->_loadBbs($bbs_id);
			$assign_data['bbs']	= $this->bbs;
			if($this->bbs->isReadable() == false)	throw new BbsPermissionException();
			if(array_key_exists($post_seq, $bpv))
			{	
				$post->load($post_seq, $this->bbs);
			}
			else
			{
				$post->read($post_seq, $this->bbs);
				$bpv[$post_seq] = true;
				$this->session->set('bpv', $bpv);
			}
			if($post['post_del_flag'] == 'T')	throw new NoRecordException();
			if($this->bbs['bbs_seq'] != $post['fk_bbs_seq'])	throw new BbsAccessException();
			
			$assign_data['data'] = $post;
			$assign_data['post_comment_secure_key']	= rand(100,999);
			$assign_data['post_comment_secure_code'] = sha1($bbs_id.$post_seq.$assign_data['post_comment_secure_key']);
			$assign_data['comment'] = $this->comment_json(true, $bbs_id, $post_seq);
			
			if($this->bbs['bbs_linked_post_flag'] == 'T')
			{
				$lm = Wdk::get('PostManager');
				$assign_data['linked_post'] = $lm->getLinkedByUser($post);
			}
			$this->keywords = $post['post_tag'];
			$this->description = $post['post_tag'];
		}
		catch(NoRecordException $e)
		{
			parent::_script(array("alert('삭제된 게시물입니다.')", "history.back();"));
		}
		catch(BbsAccessException $e)
		{
			parent::_script(array("alert('잘못된 접근입니다.')", "history.back();"));
		}
		catch(BbsPermissionException $e)
		{
			parent::_script(array("alert('접근 권한이 없습니다.')", "history.back();"));
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_view($assign_data, "/post/{$this->bbs['bbs_skin']}/view.html");
		/****************************************************************************************************************************/
	}
	
	public function captcha()
	{
		/*****************************************************************************************************************************
		* BASIC VARS
		*****************************************************************************************************************************/
		$captcha = Wdk::helper('captcha'); 
		/****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		$captcha->createImage();
		exit;
		/****************************************************************************************************************************/
	}
	
	public function download()
	{
		if(parent::_isPost())
		{
			$file_path = str_replace('//', '/', BASE_PATH. (string) post('file_path'));
			$down_name = (string) post('down_name');
				
			$ua = $_SERVER["HTTP_USER_AGENT"];
		
			$encoded_filename = urlencode($down_name);
			$encoded_filename = str_replace("+", "%20", $encoded_filename);
			if(is_file($file_path))
			{
				header("Pragma: public");
				header("Expires: 0");
				header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
				header("Cache-Control: private",false);
				header("Content-type: application/force-download");
		
				if (preg_match("/MSIE/", $ua))
				{
					header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
				}
				else if (preg_match("/Firefox/", $ua))
				{
					header('Content-Disposition: attachment; filename*="utf8\'\'' . $down_name . '"');
				}
				else
				{
					header('Content-Disposition: attachment; filename="' . $down_name . '"');
				}
		
				header("Content-Transfer-Encoding: binary");
				header("Content-Length: ".filesize($file_path));
				readfile($file_path);
			}
		}
		exit;
	}
	
	public function delete()
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$bbs_id			= (string)	post('bbs_id');
		$post_seq	= (int)	post('post_seq', 0);
		/****************************************************************************************************************************/
		try 
		{
			$this->_loadBbs($bbs_id);
			if($this->bbs->isWritable() == false)	throw new BbsPermissionException();
			$post = Wdk::get('Post');
			$post->load($post_seq);
			if($post['post_del_flag'] == 'T')	throw new NoRecordException();
			if($this->bbs['bbs_seq'] != $post['fk_bbs_seq'])
			{
				throw new BbsAccessException("");
			}
			if($this->session['user_seq'] != $post['fk_user_seq'])
			{
				throw new BbsPermissionException("");
			}
			$post->delete();
		}
		catch(NoRecordException $e)
		{
			parent::_script(array("alert('존재하지 않는 게시물입니다.')", "history.back();"));
		}
		catch(BbsAccessException $e)
		{
			parent::_script(array("alert('잘못된 접근입니다.')", "history.back();"));
		}
		catch(BbsPermissionException $e)
		{
			parent::_script(array("alert('권한이 없습니다.')", "history.back();"));
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_redirect("/post/".$this->bbs['bbs_id']);
		/****************************************************************************************************************************/
	}
	
	public function comment_pop($bbs_id, $post_seq=0)
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data = array('bbs'=>$this->bbs);
		$post 		= Wdk::get('Post');
		/****************************************************************************************************************************/
		try
		{
			$this->_loadBbs($bbs_id);
			$assign_data['bbs']	= $this->bbs;
			if($this->bbs->isReadable() == false)	throw new BbsPermissionException();
			$post->load($post_seq, $this->bbs);
			if($post['post_del_flag'] == 'T')	throw new NoRecordException();
			if($this->bbs['bbs_seq'] != $post['fk_bbs_seq'])	throw new BbsAccessException();
			
			$assign_data['data'] = $post;
			$assign_data['post_comment_secure_key']	= rand(100,999);
			$assign_data['post_comment_secure_code'] = sha1($bbs_id.$post_seq.$assign_data['post_comment_secure_key']);
			$assign_data['comment'] = $this->comment_json(true, $bbs_id, $post_seq);
		}
		catch(NoRecordException $e)
		{
			parent::_script(array("alert('존재하지 않는 게시물입니다.')", "history.back();"));
		}
		catch(BbsAccessException $e)
		{
			parent::_script(array("alert('잘못된 접근입니다.')", "history.back();"));
		}
		catch(BbsPermissionException $e)
		{
			parent::_script(array("alert('권한이 없습니다.')", "history.back();"));
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		parent::_layout('popup');
		parent::_view($assign_data, "/post/{$this->bbs['bbs_skin']}/comment_pop.html");
		/****************************************************************************************************************************/
	}
	
	public function comment()
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$captcha					= Wdk::helper('captcha');
		$bbs_id						= (string) post('bbs_id');
		$post_seq					= (int) post('post_seq');
		$comment_user_name			= (string) post('comment_user_name');
		$comment_user_passwd		= (string) post('comment_user_passwd');
		$comment_content			= (string) post('comment_content');
		$comment_captcha_code		= (string) post('comment_captcha_code');
		$post_comment_secure_key	= (int) post('post_comment_secure_key');
		$post_comment_secure_code	= (string) post('post_comment_secure_code');
		$mode						= (string) post('mode');
		$comment_seq				= (int) post('comment_seq');
		$assign_data				= array('error'=>true);
		/****************************************************************************************************************************/
		try 
		{	
			if(parent::_isPost() == false) throw new BbsAccessException();
			$this->_loadBbs($bbs_id);
			if(sha1($bbs_id.$post_seq.$post_comment_secure_key) !== $post_comment_secure_code)
			{
				throw new BbsAccessException();
			}
			if($this->bbs->isReadable() == false)	
			{
				throw new BbsPermissionException();
			}
// 			if(parent::_isLogin() == false)
// 			{
// 				if(strlen(trim($comment_user_name)) < 3)	throw new WException('이름은 2자이상 입력하십시오.');
// 				if(strlen(trim($comment_user_passwd)) < 5)	throw new WException('비밀번호는 5자이상 입력하십시오.');
// 				if($captcha->check($comment_captcha_code) == false)	throw new WException('보안코드를 잘못입력하셨습니다.');
// 			}
			$comment = Wdk::get('Comment');
			switch(strtoupper($mode))
			{
				case "MODIFY":
					$comment->load($comment_seq);
					if($comment['fk_user_seq'] 		!= $this->session['user_seq']) throw new BbsPermissionException();
					$comment['comment_content']		= $comment_content;
					$comment->update();
					$assign_data['error'] = false;
					break;
				case "REPLY":
					$comment->load($comment_seq);
					$comment->reply($this->session['user_seq'],
									$comment_content
								);
					$assign_data['error'] = false;
					break;
				case "AGREE":
					$comment->load($comment_seq);
					$comment->agree($this->session['user_seq']);
					$assign_data['error'] = false;
					break;
				case "OPPOSITE":
					$comment->load($comment_seq);
					$comment->opposite($this->session['user_seq']);
					$assign_data['error'] = false;
					break;
				default:
					$comment['fk_post_seq']			= $post_seq;
					$comment['fk_user_seq']			= $this->session['user_seq'];
					$comment['comment_index']		= 0;
					$comment['comment_depth']		= 0;
					$comment['comment_score']		= 0;
					$comment['comment_user_name']	= $comment_user_name;
					$comment['comment_user_passwd']	= $comment_user_passwd;
					$comment['comment_content']		= $comment_content;
					$comment->insert();
					$assign_data['error'] = false;
					break;
			}
		}
		catch(BbsAccessException $e)
		{
			$assign_data['msg'] = "잘못된 접근입니다.";
		}
		catch(BbsPermissionException $e)
		{
			$assign_data['msg'] = "권한이 없습니다.";
		}
		catch(WException $e)
		{
			$assign_data['msg'] = $e->getMessage();
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_json($assign_data);
		/****************************************************************************************************************************/
	}
	
	public function comment_json($return=false, $bbs_id='', $post_seq=0)
	{
		/*****************************************************************************************************************************
		 * POST PARAMS..
		*****************************************************************************************************************************/
		$page			= (int)		post('page', 1);
		$bbs_id			= (string)	post('bbs_id', $bbs_id);
		$post_seq	= (int)	post('post_seq', $post_seq);
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$this->_loadBbs($bbs_id);
		$lm = Wdk::get('CommentManager');
		$listsize = 30;
		$pagesize = 10;
		$lists = array();
		$where = array("`fk_post_seq` = {$post_seq}");
		$assign_data = array("listsize"=>$listsize, "pagesize"=>$pagesize);
		/****************************************************************************************************************************/
		try
		{
			$lm['listsize'] = $listsize;
			$lm['page'] = $page;
			$lm['where'] = $where;
			$lm['order'] = array("`comment_index` ASC");
			$lm->load();
			foreach($lm['lists'] as $idx=>$list)
			{
				$data = array(	'seq'			=>	$list['comment_seq'],
								'index'			=>	$list['comment_index'],
								'depth'			=>	$list['comment_depth'],
								'user_login_id'	=>	$list['user_login_id'],
								'modifiable'	=>	$this->session['user_seq'] ==  $list['fk_user_seq'] ? true : false,
								'user_nick'		=>	$list['user_nick_str'],
								'content'		=>	$list['comment_content'],
								'insert_time'	=>	$list['comment_insert_time_str']
							);
				array_push($lists, $data);
			}
			$assign_data['total'] = $lm['total'];
			$assign_data['lists'] = $lists;
		}
		catch(NoRecordException $e)
		{
			$assign_data['total'] = 0;
			$assign_data['lists'] = array();
		}
		catch(WException $e)
		{
			$assign_data['error'] = true;
			$assign_data['msg'] = "Error";
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/
		return parent::_json($assign_data, $return);
		/****************************************************************************************************************************/
	}
	
	public function comment_delete()
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$bbs_id			= (string)	post('bbs_id');
		$post_seq	= (int)	post('post_seq', 0);
		$comment_seq	= (int)	post('comment_seq', 0);
		$assign_data	= array('error'=>true);
		/****************************************************************************************************************************/
		try 
		{
			if(parent::_isPost() == false) throw new WException();
			if($comment_seq == 0)	throw new NoRecordException();
			$this->_loadBbs($bbs_id);
			$post = Wdk::get('Post');
			$post->load($post_seq);
			if($post['post_del_flag'] == 'T')	throw new WException('존재하지않는 게시물입니다.');
			if($this->bbs['bbs_seq'] != $post['fk_bbs_seq'])
			{
				throw new BbsAccessException();
			}
			$comment = Wdk::get('Comment');
			$comment->load($comment_seq, $this->bbs);
			if($comment->isEditable() == false)	throw new BbsPermissionException();
			$comment->delete();
			$assign_data['error']	= false;
		}
		catch(NoRecordException $e)
		{
			$assign_data['msg']	= "존재하지 않는 코멘트입니다.";
		}
		catch(BbsAccessException $e)
		{
			$assign_data['msg']	= "잘못된 접근입니다.";
		}
		catch(BbsPermissionException $e)
		{
			$assign_data['msg']	= "권한이 없습니다.";
		}
		catch(WException $e)
		{
			$assign_data['msg']	= $e->getMessage();
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/
		return parent::_json($assign_data);
		/****************************************************************************************************************************/
	}
	
	public function react()
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$mode			= (string) post('mode');
		$bbs_id			= (string) post('bbs_id');
		$post_seq		= (int) post('post_seq');
		$post			= Wdk::get('Post');
		$assign_data	= array('error'=>true);
		/****************************************************************************************************************************/
		try 
		{	
			if(parent::_isPost() == false) throw new BbsAccessException();
			$this->_loadBbs($bbs_id);
			$post->load($post_seq, $this->bbs);
			if($post->isReactable() == false)	
			{
				throw new BbsPermissionException();
			}
			switch(strtoupper($mode))
			{
				case "AGREE" :
					$post->agree();
					$assign_data['error'] = false; 
					break;
				case "OPPOSITE" : 
					$post->opposite();
					$assign_data['error'] = false;
					break;
			}
		}
		catch(BbsAccessException $e)
		{
			$assign_data['msg'] = "잘못된 접근입니다.";
		}
		catch(BbsPermissionException $e)
		{
			$assign_data['msg'] = "권한이 없습니다.";
		}
		catch(WException $e)
		{
			$assign_data['msg'] = $e->getMessage();
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_json($assign_data);
		/****************************************************************************************************************************/
	}
	
	public function scrap()
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$bbs_id			= (string) post('bbs_id');
		$post_seq		= (int) post('post_seq');
		$post			= Wdk::get('Post');
		$assign_data	= array('error'=>true);
		/****************************************************************************************************************************/
		try 
		{
			if(parent::_isPost() == false) throw new BbsAccessException();
			$this->_loadBbs($bbs_id);
			$post->load($post_seq, $this->bbs);
			$post->scrap();
			$assign_data['error'] = false;
		}
		catch(BbsAccessException $e)
		{
			$assign_data['msg'] = "잘못된 접근입니다.";
		}
		catch(BbsPermissionException $e)
		{
			$assign_data['msg'] = "이미 스크랩 저장중인 게시물 이거나 권한이 없습니다.";
		}
		catch(WException $e)
		{
			$assign_data['msg'] = $e->getMessage();
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_json($assign_data);
		/****************************************************************************************************************************/
	}
	
	public function moveUp()
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$bbs_id			= (string) post('bbs_id');
		$post_seq		= (int) post('post_seq');
		$post			= Wdk::get('Post');
		$assign_data	= array('error'=>true);
		/****************************************************************************************************************************/
		try 
		{	
			if(parent::_isPost() == false) throw new BbsAccessException();
			$this->_loadBbs($bbs_id);
			$post->load($post_seq, $this->bbs);
			$post->moveUp();
			$assign_data['error'] = false;
		}
		catch(BbsAccessException $e)
		{
			$assign_data['msg'] = "잘못된 접근입니다.";
		}
		catch(BbsPermissionException $e)
		{
			$assign_data['msg'] = "권한이 없습니다.";
		}
		catch(WException $e)
		{
			$assign_data['msg'] = $e->getMessage();
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_json($assign_data);
		/****************************************************************************************************************************/
	}
	
	public function report_post()
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$bbs_id			= (string) post('bbs_id');
		$post_seq		= (int) post('post_seq');
		$post			= Wdk::get('Post');
		$assign_data	= array('error'=>true);
		/****************************************************************************************************************************/
		try 
		{	
			if(parent::_isPost() == false) throw new BbsAccessException();
			$this->_loadBbs($bbs_id);
			$post->load($post_seq, $this->bbs);
			$post->report();
			$assign_data['error'] = false;
		}
		catch(PostAlreadyReportedException $e)
		{
			$assign_data['msg'] = "해당 게시글을 이미 신고하셨습니다.";
		}
		catch(BbsAccessException $e)
		{
			$assign_data['msg'] = "잘못된 접근입니다.";
		}
		catch(BbsPermissionException $e)
		{
			$assign_data['msg'] = "권한이 없습니다.";
		}
		catch(WException $e)
		{
			$assign_data['msg'] = $e->getMessage();
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/	
		parent::_json($assign_data);
		/****************************************************************************************************************************/
	}
}